package com.space.school.services.impl;

import com.space.school.dao.CategoryDao;
import com.space.school.dao.UserDao;
import com.space.school.dao.impl.CategoryDaoImpl;
import com.space.school.dao.impl.UserDaoImpl;
import com.space.school.domain.Category;
import com.space.school.domain.User;
import com.space.school.services.CategoryService;
import com.space.school.utils.PageBean;

import java.util.List;
import java.util.Map;

public class CategoryServiceImpl implements CategoryService {
    private UserDao userDao = new UserDaoImpl();
    CategoryDao categoryDao = new CategoryDaoImpl();

    @Override
    public PageBean<Category> search(int currentPage, int pageSize, Map<String, String[]> map) {
        int start = (currentPage - 1) * pageSize;
        List<Category> list = categoryDao.search(start, pageSize, map);
        PageBean<Category> pb = new PageBean<>();
        pb.setList(list);

        //        总数居
        int count = categoryDao.count(map);
        pb.setTotalCount(count);
//        每页数据数
        pb.setRows(pageSize);
//        当前页
        pb.setCurrentPage(currentPage);
//        总页数
//        int totalPage = (int) Math.ceil((double) list.size() / pageSize);
        int totalPage = (count % pageSize == 0 ? count / pageSize : count / pageSize + 1);
        pb.setTotalPage(totalPage);
        return pb;
    }

    @Override
    public boolean insert(String category_name, String category_type) {
        if (categoryDao.insert(category_name, category_type) > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isInsert(String category_name) {
        Category category = categoryDao.isInsert(category_name);
        if (category == null) {
            return true;
        }
        return false;
    }

    @Override
    public void delete(int category_id) {
        categoryDao.delete(category_id);

    }

    @Override
    public void update(Category category) {
        categoryDao.update(category);
    }

    @Override
    public Category findById(int category_id) {
        return categoryDao.findById(category_id);
    }
}
